/*
 * Copyright (c) 2006-2022, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author        Notes
 * 2018-12-13     balanceTWK    add sdcard port file
 * 2019-06-11     WillianChan   Add SD card hot plug detection
 */

 #include <rtthread.h>
 #include <rtdevice.h>
 #include <dfs_fs.h>
 
 #define DBG_TAG "app.port_sdcard"
 #define DBG_LVL DBG_INFO
 #include <rtdbg.h>
 
 
 static int _sdcard_mount(void)
 {
     rt_thread_mdelay(1000);
     rt_device_t device;
     device = rt_device_find("sd0");
     if (device == RT_NULL)
     {
         LOG_E("SD card device not found");
         return -1;
     }

    #ifdef BSP_SDIO_MOUNT_FATFS
    if (dfs_mount("sd0", "/sdcard", "elm", 0, 0) != 0)
    {
        LOG_W("mount to '/sdcard' failed! try to mkfs %s", "sd0");
        dfs_mkfs("elm", "sd0");
        if (dfs_mount("sd0", "/sdcard", "elm", 0, 0) == 0)
        {
            LOG_I("mount to '/sdcard' success!");
        }
    }
    LOG_I("mount to '/sdcard' success!");
    #endif

    return 0;
 }
 
 static int _sdcard_unmount(void)
 {
     rt_thread_mdelay(200);
     dfs_unmount("/");
     LOG_I("Unmount \"/sdcard\"");

     return 0;
 }
 
 INIT_APP_EXPORT(_sdcard_mount);
 